<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class User extends CI_Controller {
	
	public function __construct()
	{
		parent::__construct();
		
		switch ($this->router->method)
		{
			case 'index':
			case 'logout':
			case 'change_password':
				if ( ! $this->auth->is_logged_in())
				{
					redirect();
				}
			break;
			case 'login':
			case 'create':
				if ($this->auth->is_logged_in())
				{
					redirect();
				}
			break;
		}
	}

	public function index()
	{
		$this->template->set('breadcrumbs', array(
			array(
				'url' => site_url('user'),
				'name' => 'Strona Główna Użytkownika'
			)
		));
		$this->template->render();
	}
	
	public function login()
	{
		if ( ! $this->input->post('login_submit'))
		{
			redirect();
		}

		$logged = $this->auth->login($this->input->post('login_username'), $this->input->post('login_password'));
		switch ($logged)
		{
			case 0:
				$this->template->set_message('Pierwszy błąd', 'error');
			break;
			case 1:
				$this->template->set_message('Drugi błąd', 'error');
			break;
			case 2:
				$this->template->set_message('Udało Ci się zalogować', 'success');
			break;
			default:
				$this->template->set_message('Wystąpił nieznany błąd', 'error');
		}
		redirect();
	}
	
	public function logout()
	{
		$this->auth->logout();
		redirect();
	}
	
	public function change_password()
	{
		$this->load->helper('form');
		$this->load->library('form_validation');
		
		if ($this->input->post('password_submit'))
		{
			$this->form_validation->set_rules('password_old', 'Stare Hasło', 'trim|required|strip_tags|min_length[8]|max_length[120]');
			$this->form_validation->set_rules('password_new', 'Nowe Hasło', 'trim|required|strip_tags|min_length[8]|max_length[120]|valid_password');
			$this->form_validation->set_rules('password_conf', 'Potwierdź Hasło', 'trim|strip_tags|required|matches[password_new]');
		
			if ($this->form_validation->run() !== FALSE)
			{
				$this->load->model('users');
				$result = $this->users->change_password($this->auth->data('id'), $this->input->post('password_old'), $this->input->post('password_new'));
				if ($result === TRUE)
				{
					$this->template->set_message('Udało się zmienić hasło', 'success');
					$this->logout();
				}
				else
				{
					$this->template->set_message('Nie udało się zmienić hasła', 'error');
				}
			}
		}
		
		$this->template->render();
	}
	
	public function create()
	{
		$this->load->helper('form');
		
		if ($this->input->post('register_submit'))
		{
			$this->load->library('form_validation');
		
			$this->form_validation->set_rules('register_name', 'Imię', 'trim|required|strip_tags|min_length[3]|max_length[120]');
			$this->form_validation->set_rules('register_password', 'Hasło', 'trim|required|strip_tags|min_length[8]|max_length[120]|valid_password');
			$this->form_validation->set_rules('register_password_conf', 'Potwierdź hasło', 'trim|strip_tags|required|matches[register_password]');
		
			if ($this->form_validation->run() !== FALSE)
			{
				$this->load->model('users');
				
				$user = new stdClass;
				$user->name = $this->input->post('register_name');
				$user->password = $this->input->post('register_password');
				
				$this->users->create($user);
				$this->template->set_message('Udało się utworzyć konto', 'success');
				redirect();
			}
		}
		$this->template->render();
	}
}

/* End of file user.php */
/* Location: ./application/controllers/user.php */
